package com.feilong.edgeAI.service;

import android.os.Environment;
import android.util.Log;


import com.facebook.common.logging.LoggingDelegate;
import com.feilong.edgeAI.utils.FileUtil;
import com.feilong.mscv5plus.MessageClient;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.time.LocalDateTime;

/**
 * 理解引擎和App通信的tcp
 */
public class TcpClient {
    private static final String TAG = "TcpClient";
    public static final String TCP_SERVER_IP = "127.0.0.1";
    public static Socket socket;
    public static void close(){
        try {
            if(null!=socket){
                socket.close();

                socket = null;
            }
            Log.d(TAG, "run: socket.close()");
        } catch (Exception e) {
            Log.e(TAG, "run: "+e.getMessage());
            e.printStackTrace();
        }
    }
    public static void startClient(){
        Log.d(TAG, "startClient:socket= "+socket);
        if (socket == null) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Log.i(TAG, "启动客户端");
                        socket = new Socket(TCP_SERVER_IP, 8899);
//                        socket = new Socket("10.0.2.2", 8080);
                        Log.i(TAG, "客户端连接成功");
                        EventBus.getDefault().post(new MessageClient("success"));
                        PrintWriter pw = new PrintWriter(socket.getOutputStream());

                        InputStream inputStream = socket.getInputStream();

                        byte[] buffer = new byte[1024];
                        int len = -1;
                        while ((len = inputStream.read(buffer)) != -1) {
                            String data = new String(buffer, 0, len);
                            data = data.substring(0,data.lastIndexOf("}")+1);
                            Log.i(TAG, "收到服务器的数据---------------------------------------------:" + data);
                            EventBus.getDefault().post(new MessageClient(data));
                        }
                        Log.i(TAG, "客户端断开连接");
                        EventBus.getDefault().post(new MessageClient("error"));
                        pw.close();

                    } catch (Exception EE) {
                        EE.printStackTrace();
                        EventBus.getDefault().post(new MessageClient("error"));
                        Log.i(TAG, "客户端无法连接服务器");

                    }finally {
                        try {
                            if(null!=socket){
                                socket.close();

                                socket = null;
                            }
                            Log.d(TAG, "run: socket.close()");
                        } catch (Exception e) {
                            Log.e(TAG, "run: "+e.getMessage());
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
        }
    }

    public static void sendTcpMessage(final String msg){
//        if(!msg.contains("none")){
//            FileUtil.appendText(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "send_to_feilong.txt", msg);
//        }
        Log.d(TAG, "sendTcpMessage: "+msg);
        if (socket != null && socket.isConnected()) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        socket.getOutputStream().write(msg.getBytes());
                        socket.getOutputStream().flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }else{
            if(msg.contains("ifly_ID")){
                return;
            }
            EventBus.getDefault().post(new MessageClient("error"));
            Log.d(TAG, "sendTcpMessage: error");
        }
    }
}

